import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.EventQueue;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;

import java.awt.GridBagLayout;

import javax.swing.JDesktopPane;
import javax.swing.JButton;
import javax.swing.JTree;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JList;


public class Gui_1 extends JFrame {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Gui_1 frame = new Gui_1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Gui_1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 410);
		getContentPane().setLayout(null);
		
		final JButton btnPrint = new JButton("Print");
		btnPrint.setEnabled(false);
		btnPrint.setBounds(335, 101, 89, 23);
		getContentPane().add(btnPrint);
		
		final JLabel lblDefPrint = new JLabel("Default Printer Label");
		lblDefPrint.setBounds(22, 12, 364, 23);
		getContentPane().add(lblDefPrint);
		
		JButton btnExit = new JButton("Exit");
		
		btnExit.setBounds(317, 328, 107, 32);
		getContentPane().add(btnExit);
		
		final JButton btnShow = new JButton("Show Files");
		
		btnShow.setBounds(20, 101, 107, 23);
		getContentPane().add(btnShow);
		
		final JLabel lblInvoices = new JLabel("Default Invoices folder");
		lblInvoices.setBounds(20, 43, 366, 23);
		getContentPane().add(lblInvoices);
		lblInvoices.setText(System.getProperty("user.dir")+ "\\Invoices\\");
		
		final JButton btnStop = new JButton("Stop");
		btnStop.setEnabled(false);
		btnStop.setBounds(236, 101, 89, 23);
		getContentPane().add(btnStop);
		
		JButton btnDefFolder = new JButton("New button");
		btnDefFolder.setBounds(400, 43, 24, 23);
		getContentPane().add(btnDefFolder);
		
		JButton btnDefPrinter = new JButton("New button");
		btnDefPrinter.setBounds(400, 12, 24, 23);
		getContentPane().add(btnDefPrinter);
		
		final JList list = new JList();
		list.setBounds(22, 318, 402, -182);
		getContentPane().add(list);
		
		
		btnPrint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnShow.setEnabled(false);
				btnStop.setEnabled(true);
				String path = lblInvoices.getText();
				String str = "";
				
				
				  String files;
				  File folder = new File(path);
				  File[] listOfFiles = folder.listFiles(); 
				 
				  for (int i = 0; i < listOfFiles.length; i++) 
				  {
					  str = path;
				   if (listOfFiles[i].isFile()) 
				   {
				   files = listOfFiles[i].getName();
				       
				   		str = str +files;
				   		
				   
				   		//*
				   		if (files.endsWith(".doc") || files.endsWith(".DOC")
				    		   ||files.endsWith(".docx")||files.endsWith(".DOCX"))
				       {
				          //System.out.println(files);
						  //System.out.println(i);
						  
				   		  printFile(str);
				   		  
				        }
				        //*/
				   		
				   		
				     }
				   str = null;
				   
				  }
			}
		});
		
		
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}//ok
		});
		
		
		btnShow.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String path = lblInvoices.getText();
				String str = "";
				
				
				  String files;
				  File folder = new File(path);
				  File[] listOfFiles = folder.listFiles(); 
				 
				  for (int i = 0; i < listOfFiles.length; i++) 
				  {
					  str = path;
				   if (listOfFiles[i].isFile()) 
				   {
				   files = listOfFiles[i].getName();
				       
				   		str = str +files;
				   		
				   
				   		//*
				   		if (files.endsWith(".doc") || files.endsWith(".DOC")
				    		   ||files.endsWith(".docx")||files.endsWith(".DOCX"))
				       {
				   				
				   		  
				        }
				        //*/
				   		
				   		
				     }
				   str = null;
				   
				  }
				
				
				
				
				
				
				if(list.getModel().getSize() != 0)
				{
					//lblNewLabel.setText("-->"+textPane.getText().length()+"<--");
					btnPrint.setEnabled(true); 
				}
			}
		});
	}

	//Prints a document
	public static void printDoc(String currentFile) {
			if(currentFile == null)
			{
				System.out.println("No document given!");
			}
			else
			{	
				try 
				   {
				      Desktop desktop = null;
				      if (Desktop.isDesktopSupported()) 
				      {
				        desktop = Desktop.getDesktop();
				      }
				      
				      desktop.print(new File(currentFile));
				      System.out.println("File: "+currentFile+" printing successful!");
				   } 
				  catch (IOException ioe) 
				   {
				       System.out.println("File: "+currentFile+" printing FAILED!");

				    	ioe.printStackTrace();
				   }
			}
		}
			
	//Prints the documents in TIMOLOGIA printer
	public static boolean printFile(String filename) {  
		PrintService service = PrintServiceLookup.lookupDefaultPrintService();
		
		String str=null, defPrint = "TIMOLOGIA";	
		
		if(service != null)
		{
			str = service.getName();
			System.out.println("The deafult printer is:"+str);
			//defPrint=str;
		}
		//System.out.println(service.getName());
		//str.concat(service.getName()) ;
		
		if(str.contains(defPrint))
		{
			//showPrinters();
			printDoc(filename);
			return true;
		}
		else if(str.isEmpty())
		{
			System.out.println("No printer found - File:"+filename+" - unable to print.");
			return false;
		}
		else
		{
			System.out.println("Printer "+defPrint+" is not the default printer!");
			return false;
		}
	}

	public static String FileDirectory()
	{
		String file = System.getProperty("user.dir");
		file = file + "\\defDirectory.txt";
		String line = "";
		try {

		    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		    
		    // Uncomment the line below if you want to skip the fist line (e.g if headers)
		    // line = br.readLine();

		    while ((line = br.readLine()) != null) {

		        

		    }
		    br.close();
		    

		} catch (IOException e) {
		    System.out.println("ERROR: unable to read file " + file);
		    e.printStackTrace();   
		}
		return  line;
	}
}
